//
//  HomePageVC.m
//  BiliBiliDemo
//
//  Created by XMG on 16/4/20.
//  Copyright © 2016年 XDH. All rights reserved.
//

#import "HomePageVC.h"
#import "LiveVC.h"
#import "RecommendVC.h"
#import "DramaVC.h"
#import "BranchVC.h"

#define HPScreenWidth [UIScreen mainScreen].bounds.size.width
#define HPScreenHeight [UIScreen mainScreen].bounds.size.height
@interface HomePageVC () <UIScrollViewDelegate>

/**
 *      标题
 */
@property (weak, nonatomic) UIView *headView;
/**
 *      标题按钮
 */
@property (strong, nonatomic) NSMutableArray *titleItems;
/**
 *      子控制器
 */
@property (strong, nonatomic) NSMutableArray *childViewControllers;
/**
 *      记录被点击的标题按钮
 */
@property (weak, nonatomic) UIButton *tempBtn;
/**
 *      标题导航条
 */
@property (weak, nonatomic) UIView *barView;
/**
 *      子控制器所在的Scrollview
 */
@property (weak, nonatomic) UIScrollView *scView;
/**
 *      scrollview中显示的View
 */
@property (weak, nonatomic) UIView *currentView;
/**
 *      scrollview中循环利用的View
 */
@property (weak, nonatomic) UIView *reuseView;
@end

@implementation HomePageVC

- (NSMutableArray *)titleItems
{
    if (_titleItems == nil) {
        _titleItems = [NSMutableArray array];
    }
    return _titleItems;
}

- (NSMutableArray *)childViewControllers
{
    if (_childViewControllers == nil) {
        _childViewControllers = [NSMutableArray array];
    }
    return _childViewControllers;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    //隐藏导航条
    self.navigationController.navigationBarHidden = YES;
    
    //创建一个HeadView
    [self setupHeadView];
    
    //加载子控制器
    [self loadChildViewControllers];
    
    //设置标题按钮
    [self setupHeadItem];
    
    //创建一个ScrollVuew
    [self setupScrollView];
    
}

#pragma mark - setup
/**
 *  创建一个HeadView
 */
- (void)setupHeadView
{
    UIView *headV = [[UIView alloc] initWithFrame:CGRectMake(0, 20, HPScreenWidth, 44)];
    
    [self.view addSubview:headV];
    self.headView = headV;
}

/**
 *  加载子控制器
 */
- (void)loadChildViewControllers
{
    LiveVC *liveVC = [[LiveVC alloc] init];
    liveVC.title = @"直播";
    [self addChildViewController:liveVC];
    [self.childViewControllers addObject:liveVC];
    
    RecommendVC *recomVC = [[RecommendVC alloc] init];
    recomVC.title = @"推荐";
    [self addChildViewController:recomVC];
    [self.childViewControllers addObject:recomVC];
    
    DramaVC *draVC = [[DramaVC alloc] init];
    draVC.title = @"番剧";
    [self addChildViewController:draVC];
    [self.childViewControllers addObject:draVC];
    
    BranchVC *branVC = [[BranchVC alloc] init];
    branVC.title = @"分区";
    [self addChildViewController:branVC];
    [self.childViewControllers addObject:branVC];
}

/**
 *  设置标题按钮
 */
- (void)setupHeadItem
{
    NSInteger count = self.childViewControllers.count;//按钮数量
    
    CGFloat width = self.headView.frame.size.width / count;
    CGFloat height = self.headView.frame.size.height;
    CGFloat x = width;
    CGFloat y = 0;
    UIViewController *vc = [UIViewController new];
    
    //创建底部导航条
    UIView *barView = [[UIView alloc] init];
    barView.frame = CGRectMake(0, height - 3, width, 3);
    barView.backgroundColor = HPColorPink;
    [self.headView addSubview:barView];
    self.barView = barView;
    
    //生成按钮
    for (int i = 0; i < count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        btn.frame = CGRectMake(x * i, y, width, height);
        vc = self.childViewControllers[i];
        //设置按钮标题
        [btn setTitle:vc.title forState:UIControlStateNormal];
        //设置标题颜色
        [btn setTitleColor:HPColorGrey forState:UIControlStateNormal];
        [self.headView addSubview:btn];
        [self.titleItems addObject:btn];
        //添加点击事件
        [btn addTarget:self action:@selector(selectHeadItem:) forControlEvents:UIControlEventTouchUpInside];
        
        //默认第二个按钮为选中状态(默认显示推荐界面)
        if (i == 1) {
            [self selectHeadItem:btn];
        }
    }
    
}

/**
 *      点击标题按钮
 */
- (void)selectHeadItem:(UIButton *)btn
{
    [self.tempBtn setTitleColor:HPColorGrey forState:UIControlStateNormal];
    [btn setTitleColor:HPColorPink forState:UIControlStateNormal];
    self.tempBtn = btn;
    
    //移动导航条
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frame = self.barView.frame;
        frame.origin.x = btn.frame.origin.x;
        self.barView.frame = frame;
        
    } completion:^(BOOL finished) {
        //将子控制器的View显示到Scrollview上
        self.scView.contentOffset = CGPointMake(HPScreenWidth * btn.tag, 0);
        NSLog(@"%zd", btn.tag);
    }];
}

/**
 *      创建一个CollectionView
 */
- (void)setupCollectionView
{
    
}

/**
 *      创建一个ScrollView
 */
- (void)setupScrollView
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIScrollView *scView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.headView.frame), HPScreenWidth, HPScreenHeight - 64 - 49)];
    scView.backgroundColor = [UIColor redColor];
    CGFloat width = self.childViewControllers.count * HPScreenWidth;
    CGFloat height = scView.frame.size.height;
    scView.contentSize = CGSizeMake(width, height);
    

    //加载子控制器的View
    for (int i = 0; i < self.childViewControllers.count; i++) {
        UIViewController *vc = self.childViewControllers[i];
        vc.view.frame = CGRectMake(i * HPScreenWidth, 0, HPScreenWidth, height);
        [scView addSubview:vc.view];
    }
    
    //加载RecommendVC的View
    UIViewController *vc = self.childViewControllers[self.tempBtn.tag];
    vc.view.frame = CGRectMake(self.tempBtn.tag * HPScreenWidth, 0, HPScreenWidth, height);
    [scView addSubview:vc.view];

    
    //分页功能
    scView.pagingEnabled = YES;
    //一次只允许向一个方向拖动(水平/垂直)
    scView.directionalLockEnabled = YES;
    //关闭弹动功能
    scView.bounces = NO;
    //设置代理
    scView.delegate = self;
    
    [self.view addSubview:scView];
    self.scView = scView;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger leftI = scrollView.contentOffset.x / HPScreenWidth;
    NSInteger rightI = leftI + 1;
    //获取左边的按钮
    UIButton *btnLeft = self.titleItems[leftI];
    //获取右边按钮
    UIButton *btnRight;
    if (rightI < self.titleItems.count) {
        btnRight = self.titleItems[rightI];
    }
    
    //左边按钮标题颜色
    CGFloat offset = scrollView.contentOffset.x / SCREEN_WIDTH - leftI;
    
    UIColor *leftColor = [UIColor colorWithRed:((1 - offset) * (212.0 - 135) + 135) / 255  green:((1 - offset) * (71.0 - 135) + 135) / 255 blue:((1 - offset) * (114.0 - 135) + 135) / 255 alpha:1.0];
    [btnLeft setTitleColor:leftColor forState:UIControlStateNormal];
    //右边按钮标题颜色
    UIColor *rightColor = [UIColor colorWithRed:(offset * (212.0 - 135) + 135) / 255  green:(offset * (71.0 - 135) + 135) / 255 blue:(offset * (114.0 - 135) + 135) / 255 alpha:1.0];
    [btnRight setTitleColor:rightColor forState:UIControlStateNormal];
    
    //移动导航条
    //判断移动的方向
//    CGFloat barOffset = scrollView.contentOffset.x - self.tempBtn.tag * SCREEN_WIDTH;
//    //修改barView的位置
//    CGRect frame = self.barView.frame;
//    frame.origin.x = frame.size.width * self.tempBtn.tag + barOffset * frame.size.width / SCREEN_WIDTH;
//    self.barView.frame = frame;
    self.barView.x = scrollView.contentOffset.x * self.barView.width / SCREEN_WIDTH;
}

/**
 *      滚动完成时调用
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //获取选中标题按钮
    NSInteger page = scrollView.contentOffset.x / HPScreenWidth;
    UIButton *selButton = self.titleItems[page];
    //更新标题按钮
    [self selectHeadItem:selButton];
}
@end
